/**   
* @Title: CommentController.java 
* @Package com.thomas.controller 
* @Description: TODO(用一句话描述该文件做什么) 
* @author thomas_liu   
* @date 2017年8月2日 下午4:31:52 
* @version V1.0   
*/
package com.thomas.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.thomas.entity.Blog;
import com.thomas.entity.Comment;
import com.thomas.service.BlogService;
import com.thomas.service.CommentService;
import com.thomas.util.ResponseUtil;

/** 
 * @ClassName: CommentController 
 * @Description: 评论Controller层
 * @author thomas_liu
 * @date 2017年8月2日 下午4:31:52 
 *  
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
	
	@Autowired
	private CommentService commentService;
	
	@Autowired
	private BlogService blogService;
	
	/**
	 *添加或者修改评论 
	 * @param comment  
	 * @param imageCode
	 * @param request
	 * @param response
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public String save(Comment comment, @RequestParam("imageCode") String imageCode, HttpServletRequest request, HttpServletResponse  response,HttpSession session) throws Exception{
		String sRand = (String) session.getAttribute("sRand");
		JSONObject result = new JSONObject();
		int resultTotal = 0;
		if(!imageCode.equals(sRand)){
			result.put("success", false);
			result.put("errorInfo", "验证码填写错误!");
		}else{
			String userIp = request.getRemoteAddr();//获取用户IP
			comment.setUserIp(userIp);
			if(comment.getId() == null){
				resultTotal = commentService.add(comment);
				//博客回复次数加一
				Blog blog = blogService.findById(comment.getBlog().getId());
				blog.setReplyHit(blog.getReplyHit()+1);
				
				blogService.update(blog);
			}else{
				
			}
		}
		if(resultTotal>0){
			result.put("success", true);
		}else{
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
	
	
}
